package com.heer.gymnasium.module.coach.views;

import android.content.Context;
import android.content.Intent;
import android.widget.ListView;

import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.SignHelper;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseRefreshActivity;
import com.heer.gymnasium.module.coach.beans.CoachResponseBean;
import com.heer.gymnasium.module.coach.beans.CoachStatisticsBean;
import com.heer.gymnasium.module.coach.beans.StatisticsResponseBean;
import com.heer.gymnasium.module.evaluate.views.MemberListActivity;
import com.heer.gymnasium.util.StringUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan
 * on 2018/3/15
 * 私教统计
 */

public class CoachStatisticsActivity extends BaseRefreshActivity {
    private static final int RES_ID_LAYOUT = R.layout.layout_coach_statistics;
    private static final int RES_ID_LIST = R.id.layout_coach_statistics_lv;
    private static final int RES_ID_REFRESH = R.id.refreshLayout;

    private static final int RES_ID_ITEM = R.layout.item_statistics;
    private static final int RES_ID_NAME = R.id.item_statistics_name_tv;
    private static final int RES_ID_LESSON = R.id.item_statistics_lessonnum_tv;
    private static final int RES_ID_COUNT = R.id.item_statistics_count_tv;

    @BindView(RES_ID_LIST)
    ListView listView;
    private List<CoachStatisticsBean> data;
    private QuickTypeAdapter<CoachStatisticsBean> adapter;

    @Override
    protected void init() {
        super.init();
        setContentView(RES_ID_LAYOUT);
        setRefreshResId(RES_ID_REFRESH);
    }

    @Override
    protected void initView() {
        super.initView();
        data = new ArrayList<>();
        titleUtils.setTitleText("私教统计");
        adapter = new QuickTypeAdapter<CoachStatisticsBean>(this, data, RES_ID_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, CoachStatisticsBean data, int type, int pos) {
                holder.setText(RES_ID_NAME, data.getUsername());
                holder.setText(RES_ID_LESSON, data.getDistoryNum());
                holder.setText(RES_ID_COUNT, data.getCount());
            }
        };
        listView.setAdapter(adapter);
    }

    @Override
    protected void loadData(boolean loadMore) {
        super.loadData(loadMore);
        coachCount();
        setRefresh(false);
    }

    private void coachCount() {
        SignHelper signHelper = new SignHelper("");
        mAppHelper.apiService().coachCount(signHelper.getToken()).enqueue(new BaseCallback<ApiResponse<StatisticsResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<StatisticsResponseBean>> call, Response<ApiResponse<StatisticsResponseBean>> response) {
                int total = StringUtils.toInt(response.body().getData().getTotal());
                int current = StringUtils.toInt(response.body().getData().getCurrent_page());
                int per_page = StringUtils.toInt(response.body().getData().getPer_page());
                if (total <= current * per_page) {
                    layoutRefresh.setDirection(SwipyRefreshLayoutDirection.TOP);
                } else {
                    layoutRefresh.setDirection(SwipyRefreshLayoutDirection.BOTTOM);
                }
                if (pageNo == 1) {
                    data.clear();
                }
                data.addAll(CoachStatisticsBean.dataChange(response.body().getData().getData()));
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void setRefresh(boolean isRefreshing) {
        if (layoutRefresh.isRefreshing()) {
            layoutRefresh.setRefreshing(isRefreshing);
        }
    }

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, CoachStatisticsActivity.class);
        context.startActivity(intent);
    }
}
